package com.keelau.jxbrowser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserCore;
import com.teamdev.jxbrowser.chromium.BrowserType;
import com.teamdev.jxbrowser.chromium.internal.Environment;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.logging.Level;

import static com.teamdev.jxbrowser.chromium.LoggerProvider.setLevel;

public class JxbrowserDemo extends Application {

    ArrayList<Browser> browserArrayList=new ArrayList<Browser>();


    @Override
    public void init() throws Exception {
        // On Mac OS X Chromium engine must be initialized in non-UI thread.
        if (Environment.isMac()) {
            BrowserCore.initialize();
        }
    }

    @Override
    public void start(Stage primaryStage) {
        //setLevel(Level.ALL);
        TabPane tabPane = new TabPane();
        tabPane.getTabs().add(createBrowserTab());
        tabPane.getTabs().add(createBrowserTab());
        StackPane pane = new StackPane();
        pane.getChildren().add(tabPane);
        Scene scene = new Scene(pane, 500, 400);
        primaryStage.setTitle("JxBrowser: JavaFX - Hello World");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public  Tab createBrowserTab(){
        Tab tab = new Tab("baidu");
        Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
        BrowserView browserView = new BrowserView(browser);
        tab.setOnClosed(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                //last tag close throw exception
                //
                browser.dispose();
            }
        });

        tab.setContent(browserView);
        browser.loadURL("www.baidu.com");
        //tabPane.getTabs().add(tab);
        //browserArrayList.add(browser);
        return tab;
    }

    @Override
    public void stop() throws Exception {
        //dispose here Success
        /*browserArrayList.forEach(t->{
            t.dispose();
        });*/
        super.stop();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
